<template>
  <div class="article-container">
    <!-- 头部栏 -->
    <van-nav-bar
      title="文章详情"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    ></van-nav-bar>

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ articleInfo.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="cover"
          :src="articleInfo.aut_photo"
        />
        <div class="text">
          <p class="name">{{ articleInfo.aut_name }}</p>
          <p class="time">{{ articleInfo.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="followClick">{{
          articleInfo.is_followed === true ? '已关注' : '+ 关注'
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="articleInfo.content">正文</div>
      </div>
      <br />
      <br />
      <div class="like">
        <van-button
          size="small"
          :icon="articleInfo.attitude == 1 ? 'good-job' : 'good-job-o'"
          plain
          round
          color="#6EDDAA"
          @click="togglePraise"
          >{{ articleInfo.attitude == 1 ? '取消点赞' : '点赞' }}</van-button
        >
        &nbsp;&nbsp; &nbsp;

        <van-button
          size="small"
          :icon="articleInfo.attitude == 0 ? 'delete' : 'delete-o'"
          plain
          round
          color="#FD2F5B"
          @click="toggleDisLike"
          >{{ articleInfo.attitude == 0 ? '取消不喜欢' : '不喜欢' }}</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import {
  getArticleInfo,
  followUser,
  unFollowUser,
  addPraise,
  cancelPraise,
  cancelDisLike,
  addDisLike
} from './home'
export default {
  name: 'myArticle',
  data () {
    return {
      articleInfo: {}
    }
  },
  async created () {
    let res = await getArticleInfo({ id: this.$route.query.id })
    console.log(res)
    this.articleInfo = res.data.data
  },
  methods: {
    // 关注
    async followClick () {
      // 判断关注状态
      if (this.articleInfo.is_followed === true) {
        // 已关注 则取消
        let res = await unFollowUser({ target: this.articleInfo.aut_id })
        // console.log(res)
        this.$toast.success('取关成功')
        this.articleInfo.is_followed = false
      } else {
        // 未关注 则关注
        let res = await followUser({ target: this.articleInfo.aut_id })
        // console.log(res)
        this.$toast.success('关注成功')
        this.articleInfo.is_followed = true
      }
    },
    // 点赞
    async togglePraise () {
      // 判断是否点赞
      if (this.articleInfo.attitude == 1) {
        // 点赞就取消
        let res = await cancelPraise(this.articleInfo.art_id)
        if (res.status == 204) {
          this.$toast.success('取消点赞成功')
          this.articleInfo.attitude = -1
        } else {
          this.$toast.success('操作失败')
        }
      } else {
        // 反之亦然
        // 点击时, 要进行点赞请求
        let res = await addPraise(this.articleInfo.art_id)
        if (res.status == 201) {
          this.$toast.success('点赞成功')
          this.articleInfo.attitude = 1
        } else {
          this.$toast.success('操作失败')
        }
      }
    },

    // 不喜欢
    async toggleDisLike () {
      // 通过 attitude  0：不喜欢
      if (this.articleInfo.attitude == 0) {
        // 点击时, 要取消不喜欢
        let res = await cancelDisLike(this.articleInfo.art_id)
        if (res.status == 204) {
          this.$toast.success('取消不喜欢成功')
          this.articleInfo.attitude = -1
        } else {
          this.$toast.success('操作失败')
        }
      } else {
        // 点击时, 要进行不喜欢
        let res = await addDisLike(this.articleInfo.art_id)
        if (res.status == 201) {
          this.$toast.success('不喜欢成功')
          this.articleInfo.attitude = 0
        } else {
          this.$toast.success('操作失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  .article-loading {
    padding-top: 100px;
    text-align: center;
  }
  .error {
    padding-top: 100px;
    text-align: center;
  }
  .detail {
    padding: 10px 10px;
    .title {
      font-size: 16px;
    }
    .author {
      display: flex;
      padding: 10px 10px;
      .text {
        flex: 1;
        padding-left: 10px;
      }
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
    .content {
      font-size: 14px;
      overflow: hidden;
      white-space: pre-wrap;
      word-break: break-all;
      img {
        max-width: 100%;
        background: #f9f9f9;
      }
    }
  }
  .van-nav-bar__content {
    background: #fff;
  }
  .like {
    text-align: center;
  }
}
</style>
